<?php

namespace Admin\Models\res;

use Admin\Models\BaseModel;
use Helpers\Main;

class XhresResourcepackcontact extends BaseModel
{

    /**
     *
     * @var integer
     */
    public $iRPCId;

    /**
     *
     * @var string
     */
    public $iAppCode;

    /**
     *
     * @var integer
     */
    public $iRPId;

    /**
     *
     * @var integer
     */
    public $bDelete;

    /**
     *
     * @var string
     */
    public $tCreateDate;

    /**
     *
     * @var string
     */
    public $iCreateUserId;

    /**
     *
     * @var string
     */
    public $tLastModifyDate;

    /**
     *
     * @var string
     */
    public $iLastModifyUserId;

    /**
     * Initialize method for model.
     */
    public function initialize()
    {
        parent::initialize();
        $this->setSource("XHRes_ResourcePackContact");
    }

    /**
     * Returns table name mapped in the model.
     *
     * @return string
     */
    public function getSource()
    {
        return 'XHRes_ResourcePackContact';
    }

    /**
     * Allows to query a set of records that match the specified conditions
     *
     * @param mixed $parameters
     * @return XhresResourcepackcontact[]
     */
    public static function find($parameters = null)
    {
        return parent::find($parameters);
    }

    /**
     * Allows to query the first record that match the specified conditions
     *
     * @param mixed $parameters
     * @return XhresResourcepackcontact
     */
    public static function findFirst($parameters = null)
    {
        return parent::findFirst($parameters);
    }

    /**
     * 根据应用编号获取关联的资源包ID字符串（逗号关联）
     *
     * @param $appCode
     * @return string
     *
     * create by ck 20161219
     */
    public static function getResPackIdByAppCode($appCode)
    {
        $resPack = self::find([
            'columns' => 'iRPId',
            'conditions' => 'bDelete = 0 AND iAppCode = :iAppCode:',
            'bind'       => ['iAppCode' => $appCode]
        ])->toArray();
        if(!$resPack) return '';
        return implode(',', array_column($resPack, 'iRPId'));
    }

    /**
     * 根据资源包ID，应用编号及类型删除或添加关联记录
     *
     * @param $resPackId
     * @param $appCode
     * @param $type
     * @return bool
     *
     * create by ck 20161220
     */
    public static function contactResPackToApp($resPackId, $appCode, $type)
    {
        if(!$resPackId || !$appCode) return true;
        if($type == 'out'){
            return self::find([
                'conditions' => 'bDelete = 0 AND iAppCode = ":iAppCode:" AND iRPId IN ({idArr:array})',
                'bind'       => ['iAppCode' => $appCode, 'idArr' => $resPackId]
            ])->delete();
        }
        $userId = Main::getLoginUserInfo('userId');
        $sql = 'INSERT INTO XHRes_ResourcePackContact (iRPId,iAppCode,bDelete,tCreateDate,iCreateUserId,tLastModifyDate,iLastModifyUserId) VALUES ';
        $nowTime = date('Y-m-d H:i:s');
        $valueArr = [];
        foreach($resPackId as $value){
            $valueArr[] = "({$value},'{$appCode}',0,'{$nowTime}',{$userId},'{$nowTime}',{$userId})";
        }
        $sql .= implode(',', $valueArr);
        return \Phalcon\Di::getDefault()->get('db')->execute($sql);
    }

}
